package model;

import entity.Book;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Noi
 */
public class MostBorrowedBookTableModel extends AbstractTableModel {

    private manager.Manager manager;
    private ArrayList<Book> alBook;

    public MostBorrowedBookTableModel(manager.Manager manager) {
        this.manager = manager;
        this.alBook = new ArrayList<Book>();
    }

    public void setAlBook(ArrayList<Book> alBook) {
        this.alBook = alBook;
        this.fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return this.alBook.size();
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Book book = this.alBook.get(rowIndex);
        int count = 0;
        for (int i = 0; i < this.manager.getOrdersDetailsManager().size(); i++) {
            if (book.getBookID() == this.manager.getOrdersDetailsManager().get(i).getBookID()) {
                count++;
            }
        }

        switch (columnIndex) {
            case 0:
                return book.getTitle();
            case 1:
                return count;
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Book Name";
            case 1:
                return "Number of Borrowed";
            default:
                return null;
        }
    }

    public Book get(int index) {
        return this.alBook.get(index);
    }
}